package com.tanhua.sso.controller;


import com.tanhua.common.pojo.User;
import com.tanhua.sso.service.UserService;
import com.tanhua.sso.vo.ErrorResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("user")
@Slf4j
public class UserController {
    @Autowired
   private UserService userService;

    /**
     * 用户登录
     * @param param
     * @return
     */
    @PostMapping("loginVerification")
    public ResponseEntity<Object> login(@RequestBody Map<String,String>param)
    {
        try {
            String phone=param.get("phone");
            String code=param.get("verificationCode");
            String login=userService.login(phone,code);
            String[] split = StringUtils.split(login, "|");
            HashMap<String, Object> objectObjectHashMap = new HashMap<>();
            objectObjectHashMap.put("token",split[0]);
            objectObjectHashMap.put("isNew",Boolean.valueOf(split[1]));
            return ResponseEntity.ok(objectObjectHashMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        ErrorResult errorResult=ErrorResult.builder().errCode("000002").errMessage("短信验证码发送失败！").build();
        return ResponseEntity.status(500).body(errorResult);
    }

    @GetMapping("{token}")
    public User queryUserByToken(@PathVariable("token") String token) {
        return this.userService.queryUserByToken(token);
    }

}
